package com.example.adminsystem.entity;

import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Data
@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class) // 添加实体监听器,  用于自动填充创建时间和更新时间
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;

    @JsonIgnore // 新增注解阻止序列化，防止密码泄露，get请求时不返回password字段
    private String password;

    private String nickname;

    private String email;

    @CreatedDate // 自动填充创建时间
    private Date createdAt;

    @LastModifiedDate // 自动填充更新时间
    private Date updatedAt;

    // 必须实现的UserDetails方法
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList(); // 如果无角色权限返回空集合
    }

    // 确保以下方法全部正确实现
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() { return true; }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() { return true; }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() { return true; }

    @Override
    @JsonIgnore
    public boolean isEnabled() { return true; }
}